package com.jinxin.platform.cdfaceacs.utils;

import lombok.extern.slf4j.Slf4j;
import org.springframework.util.LinkedMultiValueMap;
import org.springframework.util.MultiValueMap;
import org.springframework.util.StringUtils;

import java.beans.BeanInfo;
import java.beans.Introspector;
import java.beans.PropertyDescriptor;
import java.lang.reflect.Method;
import java.math.BigDecimal;
import java.util.*;

@Slf4j
public class Utils {

    public static String getUuid() {
        UUID uuid = UUID.randomUUID();
        return uuid.toString().replace("-", "");
    }
    /**
     * 获取位数为7位的随机数
     * @return
     * */
    public static String getRandom() {
        Random random = new Random();
        int nextInt = random.nextInt(9000000);
        nextInt=nextInt+1000000;
        return nextInt + "";
    }

    public static String getRandom6() {
        Random random = new Random();
        int nextInt = random.nextInt(900000);
        nextInt = nextInt + 100000;
        return nextInt + "";
    }

    /**
     * 获得21位的主键值，主键值的格式是14位的当前时间加7位的随机数
     * @return
     **/
    public static String getMillPrimaryKey() {
        return DateUtil.getCurrentYMDHMS()+getRandom();
    }

    // Bean --> Map 1: 利用Introspector和PropertyDescriptor 将Bean --> Map
    public static MultiValueMap<String, Object> transBean2Map(Object obj) {
        if (obj == null) {
            return null;
        }
        MultiValueMap<String, Object> map = new LinkedMultiValueMap<>();
        try {
            BeanInfo beanInfo = Introspector.getBeanInfo(obj.getClass());
            PropertyDescriptor[] propertyDescriptors = beanInfo.getPropertyDescriptors();
            for (PropertyDescriptor property : propertyDescriptors) {
                String key = property.getName();
                // 过滤class属性
                if (!key.equals("class")) {
                    // 得到property对应的getter方法
                    Method getter = property.getReadMethod();
                    Object value = getter.invoke(obj);
                    if(!StringUtils.isEmpty(value)){
                        map.add(key, value);
                    }
                }

            }
        } catch (Exception e) {
            log.error("transBean2Map Error {0}" ,e);
        }
        return map;
    }

    public static Map<String, String> transformBean2Map(Object obj) {
        if (obj == null) {
            return null;
        }
        Map<String, String> map = new HashMap<>();
        try {
            BeanInfo beanInfo = Introspector.getBeanInfo(obj.getClass());
            PropertyDescriptor[] propertyDescriptors = beanInfo.getPropertyDescriptors();
            for (PropertyDescriptor property : propertyDescriptors) {
                String key = property.getName();
                // 过滤class属性
                if (!key.equals("class")) {
                    // 得到property对应的getter方法
                    Method getter = property.getReadMethod();
                    Object value = getter.invoke(obj);
                    if(!StringUtils.isEmpty(value)){
                        map.put(key, String.valueOf(value));
                    }
                }

            }
        } catch (Exception e) {
            log.error("transformBean2Map Error {0}" ,e);
        }
        return map;
    }

    public static HashMap transformBean2HashMap(Object obj) {
        HashMap map = new HashMap<>();
        if (obj == null) {
            return map;
        }
        try {
            BeanInfo beanInfo = Introspector.getBeanInfo(obj.getClass());
            PropertyDescriptor[] propertyDescriptors = beanInfo.getPropertyDescriptors();
            for (PropertyDescriptor property : propertyDescriptors) {
                String key = property.getName();
                // 过滤class属性
                if (!key.equals("class")) {
                    // 得到property对应的getter方法
                    Method getter = property.getReadMethod();
                    Object value = getter.invoke(obj);
                    if(!StringUtils.isEmpty(value)){
                        map.put(key, String.valueOf(value));
                    }
                }

            }
        } catch (Exception e) {
            log.error("transformBean2Map Error {0}" ,e);
        }
        return map;
    }

    /**
     * 根据身份证号计算年龄
     * @param pensonnelIdCard
     * @return
     */
    public static Integer getPersonAgeFromIdCard(String pensonnelIdCard) {

        //截取身份证中出行人出生日期中的年、月、日
        Integer personYear = Integer.parseInt(pensonnelIdCard.substring(6, 10));
        Integer personMonth = Integer.parseInt(pensonnelIdCard.substring(10, 12));
        Integer personDay = Integer.parseInt(pensonnelIdCard.substring(12, 14));

        Calendar cal = Calendar.getInstance();
        // 得到当前时间的年、月、日
        Integer yearNow = cal.get(Calendar.YEAR);
        Integer monthNow = cal.get(Calendar.MONTH) + 1;
        Integer dayNow = cal.get(Calendar.DATE);

        // 用当前年月日减去生日年月日
        Integer yearMinus = yearNow - personYear;
        Integer monthMinus = monthNow - personMonth;
        Integer dayMinus = dayNow - personDay;

        Integer age = yearMinus; //先大致赋值

        if (yearMinus == 0) { //出生年份为当前年份
            age = 0;
        }else{ //出生年份大于当前年份
            if (monthMinus < 0) {//出生月份小于当前月份时，还没满周岁
                age = age - 1;
            }
            if (monthMinus == 0) {//当前月份为出生月份时，判断日期
                if (dayMinus < 0) {//出生日期小于当前月份时，没满周岁
                    age = age - 1;
                }
            }
        }
        return age;
    }

    /**
     *
     * @param a 除数
     * @param b 被除数
     * @param scale 保留位数
     * @return 返回
     */
    public static double divide(int a,int b,int scale){
        if(b==0){return 0;}
        return new BigDecimal((float)a/b).setScale(scale, BigDecimal.ROUND_HALF_UP).doubleValue();
    }

}
